import { instance as http } from '@/utils/http';
// 添加网络请求类型约束
import type { User, Data, UserInfo, PatientList, Patient } from '@/types/user';
// 封装用户相关的网络请求

// 密码登陆的网络请求
export const loginByPassword = async (mobile: string, password: string) => {
  const res = await http.post<User, Data<User>>('/login/password', {
    mobile: mobile,
    password: password,
  });
  return res.data;
};
// 获取验证码
export const getSMSCode = async (mobile: string, type: string) => {
  // 发起获取code请求
  const res = await http.get<User, Data<User>>('/code', { params: { mobile, type } });
  console.log('axios返回', res);
  return res.data;
};
// 验证码登录
export const loginByCode = async (mobile: string, code: string) => {
  // 发起登录请求
  const res = await http.post('/login', { mobile, code });
  // 返回响应数据
  return res.data;
};
// 获取用户信息
export const getuser = async () => {
  const res = await http.get<UserInfo, Data<UserInfo>>('/patient/myUser');
  return res.data;
};
// 获取家庭档案信息
export const getPatientList = async () => {
  const res = await http.get<PatientList, Data<PatientList>>('/patient/mylist');
  return res.data;
};
// 添加患者档案信息
export const addPatient = async (patient: Patient) => {
  // 发请求
  const res = await http.post('/patient/add', patient);
  return res.data;
};
// 编辑患者
export const putPatient = async (patient: Patient) => {
  const res = await http.put('/patient/update', patient);
  return res.data;
};
// 删除患者
export const deletePatient = async (id: string) => {
  const res = await http.delete(`/patient/del/${id}`);
  return res.data;
};
// 获取患者信息
export const getPatient = async (id: string) => {
  // 发起请求
  const res = await http.get<Patient, Data<Patient>>(`/patient/info/${id}`);
  return res.data;
};
// QQ登陆
export const loginByQQ = async (openId: string) => {
  // 发请求
  const res = await http.post<User, Data<User>>('/login/thirdparty', { openId, source: 'qq' });
  return res.data;
};
// 绑定手机号
export const bindMobile = async (data: { mobile: string; code: string; openId: string }) => {
  // 发起请求
  const res = await http.post<User, Data<User>>('/login/binding', data);
  return res.data;
};
